package com.codechuan.hallo_internal.adapter;

import static android.os.Looper.getMainLooper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codechuan.hallo_internal.R;
import com.codechuan.hallo_internal.entity.SuccessOrder;

import java.util.List;
import java.util.UUID;

/**
 * @Deprecated 订单适配器
 */
public class OrderBaseAdapter extends BaseAdapter {
    private Handler handler;

    private Context context;

    private List<SuccessOrder> successOrders;


    public OrderBaseAdapter(Context context, List<SuccessOrder> successOrders) {
        this.context = context;
        this.successOrders = successOrders;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        // 获取总回显数
        return successOrders.size();
    }

    @Override
    public Object getItem(int i) {
        return successOrders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 获取主线程
        handler = new Handler(getMainLooper());
        OrderViewHolder viewHolder;
        if (view == null) {
            // 获取布局样式内容,根据布局***.xml 生成视图对象
            view = LayoutInflater.from(context).inflate(R.layout.item_order, null);
            viewHolder = new OrderViewHolder();
            viewHolder.ll_item = view.findViewById(R.id.order_ll_item);
            viewHolder.order = view.findViewById(R.id.ordertext);
            view.setTag(viewHolder);
        } else {
            viewHolder = (OrderViewHolder) view.getTag();
        }

        //  blocksDescendants 阻止下面的控件获得焦点
        viewHolder.ll_item.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        SuccessOrder successOrder = successOrders.get(i);
        // 设置order回显格式
        String tokenName = "订单" + (i + 1) + ":" + successOrder.getCarPoolText() + "\t" + successOrder.getPrice() + "元" + "(" + successOrder.getPersonNumber() + "人)"
                + "\n" + "起点:" + successOrder.getStartAddress() + "\n" + "终点:" + successOrder.getEndAddress() + "\n" + "距离乘客位置:" + successOrder.getStartDes() + "公里\t全程:" + successOrder.getOrderDes() + "公里"
                + "\n成功加入PK,等待PK结果中......."
                + "\n采用大数据库TOKEN:" + UUID.randomUUID().toString().replaceAll("-", "") + "获取的订单"
                + "\n========================";
        viewHolder.order.setText(tokenName);

        return view;
    }

    public static String maskString(String str) {
        if (str == null || str.length() <= 10) {
            return str;
        }
        return str.substring(0, 8) + "******";
    }

    public static final class OrderViewHolder {
        public LinearLayout ll_item;

        public TextView order;


    }
}
